/**
 * 
 */
package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Repository;


import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.design.DesignPattern;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IDesignPatternDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;

/**
 * @author longshaohang
 * 
 */

@Repository("designPatternDao")
public class DesignPatternDaoImpl implements IDesignPatternDao {

	@Resource(name = "baseDao")
	private IBaseDao<DesignPattern> baseDao;

	@Override
	public void save(DesignPattern designPattern) {
		baseDao.save(designPattern);
	}

	@Override
	public DesignPattern update(DesignPattern designPattern) {
		return baseDao.update(designPattern);
	}

	@Override
	public DesignPattern queryById(long id) {
		return baseDao.queryById(DesignPattern.class, id);
	}

	@Override
	public DesignPattern queryByName(String name) {
		StringBuffer hql = new StringBuffer();
		hql.append("from DesignPattern dp where dp.name = '");
		hql.append(name);
		hql.append("'");
		return baseDao.queryObject(hql.toString());
	}

	@Override
	public PageBean queryByPageNum(int pageNum) throws DBException {
		PageBean<DesignPattern> pgBean = baseDao.queryByHqlForPage(pageNum, Const.PAGESIZE,"DesignPattern");
		return pgBean;
	}

	@Override
	public List<DesignPattern> queryByList() {
		return baseDao.queryList("DesignPattern");
	}

	@Override
	public DesignPattern queryByUri(String uri) {
		StringBuffer hql = new StringBuffer();
		hql.append("from DesignPattern dp where dp.uri='");
		hql.append(uri);
		hql.append("'");
		return baseDao.queryObject(hql.toString());
	}
	
	@Override
	public boolean checkUri(String uri) {
		DesignPattern dp = queryByUri(uri);
		if (dp != null) {
			return false;
		}else{
			return true;
		}
	}

	@Override
	public DesignPattern queryByType(String type) {
		StringBuffer hql = new StringBuffer();
		hql.append("from DesignPattern dp where dp.type='");
		hql.append(type);
		hql.append("'");
		return baseDao.queryObject(hql.toString());
	}


}
